package com.up366.mobile.common.views;

import android.animation.ValueAnimator;
import android.content.Context;
import android.view.View;

import com.airbnb.lottie.LottieAnimationView;
import com.tian.scroll.R;
import com.ylw.pullrefreshlibrary.PullRefreshLayout;
import com.ylw.pullrefreshlibrary.refreshview.IRefreshView;

/**
 * Created by wangjing on 2016/6/30.
 */
public class RefreshView implements IRefreshView {

    private LottieAnimationView animationView;
    private float step1 = 0.03f;
    private float step2 = 0.031f;
    private float step3 = 0.954f;
    private int state;

    private ValueAnimator.AnimatorUpdateListener step2Listener = new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            if (animationView.getProgress() > step2) {
                animationView.pauseAnimation();
            }
        }
    };
    private ValueAnimator.AnimatorUpdateListener step3Listener = new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            if (animationView.getProgress() >= step3) {
                animationView.pauseAnimation();
                animationView.setProgress(step2);
                animationView.resumeAnimation();
            }
        }
    };

    @Override
    public void init(Context context, PullRefreshLayout pullRefreshLayout) {
        View.inflate(context, R.layout.custom_refresh_head_layout, pullRefreshLayout);
    }

    @Override
    public void initView(View headView) {
        animationView = headView.findViewById(R.id.animationView);
        animationView.setSpeed(1);
    }

    // 下拉刷新
    @Override
    public void toStep1(Context context, int lastState, int state) {
        this.state = state;
        animationView.removeUpdateListener(step2Listener);
        animationView.removeUpdateListener(step3Listener);
//        if (lastState== PullRefreshLayout.PullCallBack.STATE_STEP2){
//
//        }
    }

    // 松手刷新
    @Override
    public void toStep2(Context context, int lastState, int state) {
//        animationView.removeUpdateListener(step2Listener);
//        animationView.removeUpdateListener(step3Listener);
//        this.state = state;
//        if (animationView.isAnimating()) {
//            animationView.pauseAnimation();
//        }
        animationView.resumeAnimation();
//        animationView.addAnimatorUpdateListener(step2Listener);
        animationView.setRepeatCount(ValueAnimator.INFINITE);
    }

    // 正在刷新
    @Override
    public void toStep3(Context context, int lastState, int state) {
//        animationView.removeUpdateListener(step2Listener);
//        animationView.removeUpdateListener(step3Listener);
//        this.state = state;
//        if (!animationView.isAnimating()) {
//            animationView.resumeAnimation();
//        }
//        animationView.addAnimatorUpdateListener(step3Listener);
    }

    // 刷新完成
    @Override
    public void toStep4(Context context) {
//        animationView.removeUpdateListener(step2Listener);
//        animationView.removeUpdateListener(step3Listener);
//        this.state = -1;
//        if (animationView.isAnimating()) {
//            animationView.pauseAnimation();
//        }
//        animationView.setProgress(step3);
//        animationView.resumeAnimation();
        animationView.setRepeatCount(0);
    }

    // 上次刷新时间
    public void setRefreshTime(String time) {
    }

    @Override
    public void updatePercent(float percent_) {
        if (animationView.isAnimating()) {
            animationView.pauseAnimation();
        }
        float progress = step1 * percent_;
        animationView.setProgress(progress);
    }

}
